package com.atwork.wuhua.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.atwork.wuhua.ConstantsMyself;
import com.atwork.wuhua.R;
import com.atwork.wuhua.adapter.EvaluateAdapter;
import com.atwork.wuhua.base.BaseActivity;
import com.atwork.wuhua.bean.EvaluateBean;
import com.atwork.wuhua.bean.EvaluateDetailBean;
import com.atwork.wuhua.mvp.presenter.EvaluatePresenter;
import com.atwork.wuhua.mvp.view.IEvaluateActivity;
import com.atwork.wuhua.utils.LogUtils;
import com.atwork.wuhua.utils.StringUtil;
import com.atwork.wuhua.wrideg.StarBar;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EvaluateActivity extends BaseActivity implements IEvaluateActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.sb_lever)
    StarBar sbLever;
    @BindView(R.id.rv_evaluate)
    RecyclerView rvEvaluate;
    @BindView(R.id.ed_evaluate)
    EditText edEvaluate;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_action)
    TextView tvAction;

    private EvaluatePresenter presenter;

    private String id;
    private boolean isEvaluate; //是否已经评价  已评价不能修改数据
    private EvaluateAdapter evaluateAdapter;
    private List<EvaluateBean.DataBean> evaluateBeans;
    private StringBuffer sb = new StringBuffer();
    private float star;
    private String name;
    private List<String> labels = new ArrayList<>();


    @Override
    protected boolean isBindEventBus() {
        return false;
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_evaluate;
    }

    @Override
    public void initView() {
        tvTitle.setText("评价");
        if (getIntent().getExtras() != null) {
            id = getIntent().getExtras().getString(ConstantsMyself.INTENTID);
            isEvaluate = getIntent().getExtras().getBoolean(ConstantsMyself.INTENTFLAG);
            name = getIntent().getExtras().getString(ConstantsMyself.INTENTNAME);
            tvName.setText(name);
        }

        presenter = new EvaluatePresenter();
        presenter.attachView(this);
        presenter.getLabels();
        evaluateBeans = new ArrayList<>();

        GridLayoutManager manager = new GridLayoutManager(this, 3);
        rvEvaluate.setLayoutManager(manager);
        evaluateAdapter = new EvaluateAdapter(this, R.layout.item_evaluate, evaluateBeans);
        rvEvaluate.setAdapter(evaluateAdapter);

        evaluateAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (!isEvaluate) {
                    switch (view.getId()) {
                        case R.id.tv_tag:
                            if (evaluateBeans.get(position).isSelect()) {
                                evaluateBeans.get(position).setSelect(false);
                            } else {
                                evaluateBeans.get(position).setSelect(true);
                            }
                            evaluateAdapter.setNewData(evaluateBeans);
                            getLabels(evaluateBeans);
                            break;
                    }
                }
            }
        });
    }

    public void getLabels(List<EvaluateBean.DataBean> evaluateBeans) {
        int total = 0;
        sb.delete(0, sb.toString().length());
        for (int i = 0; i < evaluateBeans.size(); i++) {
            if (evaluateBeans.get(i).isSelect()) {
                total = total + 1;
                if (total == 1) {
                    sb.append(evaluateBeans.get(i).getId());
                } else {
                    sb.append("," + evaluateBeans.get(i).getId());
                }
            }
        }
    }

    @Override
    public void setLabsls(EvaluateBean bean) {
        evaluateBeans.addAll(bean.getData());
        evaluateAdapter.setNewData(evaluateBeans);
        if (isEvaluate) {
            presenter.getEvaluateDetail(id);
        }
    }

    @Override
    public void evaluateResult() {
        finish();
    }

    @Override
    public void setEvaluateDetail(EvaluateDetailBean bean) {
        sbLever.setStarMark(Float.parseFloat(bean.getData().getStar()));
        labels.addAll(StringUtil.getAllInSymbol(bean.getData().getLabels()));
        for (int i = 0; i < evaluateBeans.size(); i++) {
            for (int j = 0; j < labels.size(); j++) {
                if (evaluateBeans.get(i).getId().equals(labels.get(j))) {
                    evaluateBeans.get(i).setSelect(true);
                }
            }
        }
        evaluateAdapter.setNewData(evaluateBeans);
    }

    @Override
    public void initData() {
        if (isEvaluate) {
            sbLever.setIsOnTouch(false);
            sbLever.setStarMark(3);
            edEvaluate.setEnabled(false);
            edEvaluate.setFocusable(false);
            edEvaluate.setKeyListener(null);//重点
            tvAction.setVisibility(View.GONE);
        }

        sbLever.setIntegerMark(true);
        sbLever.setOnStarChangeListener(new StarBar.OnStarChangeListener() {
            @Override
            public void onStarChange(float mark) {
                star = mark;
            }
        });
    }


    @OnClick({R.id.img_left, R.id.tv_action})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_left:
                finish();
                break;
            case R.id.tv_action:
                presenter.postEvaluate(id, star, sb.toString());
                break;
        }
    }


}
